#coding=utf-8
import md5
from django.conf import settings
from django.template import Node, NodeList, Template, Context, Variable
from django.template import TemplateSyntaxError, VariableDoesNotExist, BLOCK_TAG_START, BLOCK_TAG_END, VARIABLE_TAG_START, VARIABLE_TAG_END, SINGLE_BRACE_START, SINGLE_BRACE_END, COMMENT_TAG_START, COMMENT_TAG_END


register = template.Library()
_READ_MORE_TAG = getattr(settings, 'READ_MORE_TAG', '<!--more-->')
_READ_MORE_TEXT = getattr(settings, 'READ_MORE_TEXT', 'Read More')

@register.filter('read_more')
def read_more(body, absolute_url):
    """
    {% load common_tags %}
    {{ entry.body|read_more:entry.get_absolute_url }}
    """
    if _READ_MORE_TAG in body:
        return '%s<a href="%s">%s</a>' % (body[:body.find(_READ_MORE_TAG)], absolute_url, _READ_MORE_TEXT)
    else:
        return body


class SwitchNode(template.Node):

    def __init__(self, var1, nodelist_true, nodelist_false):
        self.comparison_base = Variable(var1)
        self.nodelist_true = nodelist_true
        self.nodelist_false = nodelist_false
        self.varname = var1

    def __repr__(self):
        return "<SwitchNode>"

    def render(self, context):
        try:
            val1 = self.comparison_base.resolve(context)
            bhash = "__%s__" % md5.new(self.varname).hexdigest()
            context[bhash] = val1
        except VariableDoesNotExist:
            raise TemplateSyntaxError("Could not resolve variable %r in current context" % \
                                      self.comparison_base.var)
        ok = False
        for node in self.nodelist_true:
            if isinstance(node, CaseNode):
                node.set_source(bhash)
                if node.get_bool(context):
                    ok = True
        if ok:
            return self.nodelist_true.render(context)
        else:
            return self.nodelist_false.render(context)

@register.tag
def switch(parser, token):
    """
    Create a context to use case-like coditional
    template rendering.

    For example::

        {% switch person.name %}
            {% case 'John Doe' %}
                Hi! My name is John, the master!
            {% endcase %}
            {% case 'Mary Jane' %}
                Hello! My name is Mary. Nice to meet you!
            {% endcase %}
        {% default %}
            Oh my God! I have no name!
        {% endswitch %}
    """

    bits = list(token.split_contents())
    if len(bits) != 2:
        raise TemplateSyntaxError("%r takes one argument" % bits[0])
    end_tag = 'end' + bits[0]
    nodelist_true = parser.parse(('default', end_tag, ))
    token = parser.next_token()
    if token.contents == 'default':
        nodelist_false = parser.parse((end_tag, ))
        parser.delete_first_token()
    else:
        nodelist_false = NodeList()
    return SwitchNode(bits[1], nodelist_true, nodelist_false)


class CaseNode(Node):

    def __init__(self, var, nodelist):
        self.var = Variable(var)
        self.nodelist = nodelist

    def __repr__(self):
        return "<CaseNode>"

    def set_source(self, var):
        """ Sets the varname to lookup in
        context and make the comparisons"""
        self.base_comparison = var

    def get_bool(self, context):
        try:
            val = self.var.resolve(context)
        except VariableDoesNotExist:
            val = None

        base_comparison = getattr(self, "base_comparison", None)
        if not base_comparison:
            raise LookupError("Could not find base_comparison. "
                              "Ensure to use {% case %} node "
                              "within a {% switch %} node")
        if context.get(self.base_comparison, None) == val:
            return True
        else:
            return False

    def render(self, context):
        if self.get_bool(context):
            return self.nodelist.render(context)
        else:
            return NodeList().render(context)


@register.tag
def case(parser, token):
    bits = list(token.split_contents())
    if len(bits) != 2:
        raise TemplateSyntaxError("%r takes one argument" % bits[0])
    end_tag = 'end' + bits[0]
    nodelist_true = parser.parse(('else', end_tag))
    token = parser.next_token()
    if token.contents == 'else':
        nodelist_false = parser.parse((end_tag, ))
        parser.delete_first_token()
    else:
        nodelist_false = NodeList()
    return CaseNode(bits[1], nodelist_true)
